<?php

namespace App\Kuafu\Shop\Controller\Merchant;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Shop\Model\ShopDistributionModel;
use App\Kuafu\Shop\Service\ShopDistributionService;
use App\Kuafu\User\Service\AuthService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ShopDistributionController extends BaseController
{

    /**
     * 渠道新增
     * @param Request $request
     * @return JsonResponse
     */
    public function add(Request $request): JsonResponse
    {
        $this->validate($request, [
            'partnerName'         => 'required|string',
            'provinceCode'        => 'int',
            'cityCode'            => 'int',
            'countyCode'          => 'int',
            'detailInfo'          => 'required|string',
            'cooperationLeader'   => 'string',
            'mobile'              => 'string',
            'ratio'               => 'int',//分佣比例:仅数字1-80
            'settlementCycleType' => "required|int|in:" . implode(',',
                    array_keys(ShopDistributionModel::CYCLE_TYPE_MAPPING)),
            'minDeliveryAmount'   => 'required|int', // 单笔订单起送金额
        ]);

        $shopId = (int)$request->headers->get("shopId", 0);
        $userId = AuthService::getUserId();
        return $this->success(ShopDistributionService::addData($request, $shopId, $userId));
    }

    /**
     * 渠道编辑
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(Request $request, int $id): JsonResponse
    {
        $this->validate($request, [
            'partnerName'         => 'required|string',
            'provinceCode'        => 'int',
            'cityCode'            => 'int',
            'countyCode'          => 'int',
            'detailInfo'          => 'required|string',
            'cooperationLeader'   => 'string',
            'mobile'              => 'string',
            'ratio'               => 'int',//分佣比例:仅数字1-80
            'settlementCycleType' => "required|int|in:" . implode(',',
                    array_keys(ShopDistributionModel::CYCLE_TYPE_MAPPING)),
            'minDeliveryAmount'   => 'required|int', // 单笔订单起送金额
        ]);

        $shopId = (int)$request->headers->get("shopId", 0);
        $userId = AuthService::getUserId();
        ShopDistributionService::updateData($request, $shopId, $userId, $id);
        return $this->success();
    }

    /**
     * 渠道详情
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function info(Request $request, int $id): JsonResponse
    {
        $shopId = (int)$request->headers->get("shopId", 0);
        return $this->success(ShopDistributionService::info($shopId, $id));
    }

    /**
     * 渠道列表
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request): JsonResponse
    {
        $shopId   = (int)$request->headers->get("shopId", 0);
        $pageSize = (int)$request->get('pageSize', 20);
        $pageNum  = (int)$request->get('pageNum', 1);
        return $this->success(ShopDistributionService::list($shopId, $pageSize, $pageNum));
    }

    /**
     * 渠道删除
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function delete(Request $request, int $id): JsonResponse
    {
        $shopId = (int)$request->headers->get("shopId", 0);
        $userId = AuthService::getUserId();
        ShopDistributionService::delete($shopId, $userId, $id);
        return $this->success();
    }

    /**
     * 渠道状态变更
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function updateStatus(Request $request, int $id): JsonResponse
    {
        $this->validate($request, [
            'status' => "required|int|in:" . implode(',', array_keys(ShopDistributionModel::STATUS_MAPPING)),
        ]);

        $shopId = (int)$request->headers->get("shopId", 0);
        $userId = AuthService::getUserId();
        ShopDistributionService::updateStatus($request, $shopId, $userId, $id);
        return $this->success();
    }

    /**
     * 渠道分佣列表
     * @param Request $request
     * @return JsonResponse
     */
    public function historyBillList(Request $request): JsonResponse
    {
        $shopId         = (int)$request->headers->get("shopId", 0);
        $distributionId = (int)$request->get('distributionId', 20);

        return $this->success(ShopDistributionService::historyBillList($shopId, $distributionId));
    }

    /**
     * 渠道生成二维码
     * @param Request $request
     * @return JsonResponse
     * @throws \Exception
     */
    public function genDistributionOrderCode(Request $request): JsonResponse
    {
        $shopId         = (int)$request->headers->get("shopId", 0);
        $distributionId = (int)$request->get('distributionId', 20);
        $userId         = AuthService::getUserId();
        ShopDistributionService::genDistributionOrderCode($shopId, $distributionId, $userId);
        return $this->success();
    }

    /**
     * 渠道列表无分页-搜索用
     * @param Request $request
     * @return JsonResponse
     */
    public function searchList(Request $request): JsonResponse
    {
        $shopId   = (int)$request->headers->get("shopId", 0);
        return $this->success(ShopDistributionService::searchList($shopId));
    }
}
